// Mudos.cpp : Implementation of CMudosApp and DLL registration.

#include "stdafx.h"
#include "m.h"
/////////////////////////////////////////////////////////////////////////////
//

STDMETHODIMP CMudos::InterfaceSupportsErrorInfo(REFIID riid)
{
	static const IID* arr[] = 
	{
		&IID_IMudOS,
	};

	for (int i=0;i<sizeof(arr)/sizeof(arr[0]);i++)
	{
		if (InlineIsEqualGUID(*arr[i],riid))
			return S_OK;
	}
	return S_FALSE;
}

STDMETHODIMP CMudos::get_CurrentObject(IMObject * * pVal)
{
	// TODO: Add your implementation code here
	CComObject<CMValue> *o;
	svalue_t st;
	o=new CComObject<CMValue>;
	st.type=T_OBJECT;
	st.u.ob=get_current_object();
	o->Create(&st);
	o->AddRef();
	*pVal=o;
	return S_OK;
}

STDMETHODIMP CMudos::get_CurrentProgram(IMProgram * * pVal)
{
	// TODO: Add your implementation code here
	CComObject<CMProgram> *o;
	o=new CComObject<CMProgram>;
	o->Create(get_current_object());
	o->AddRef();
	*pVal=o;
	return S_OK;
}

typedef int (*FPCOMMHOOK)(char *buf,int cursize,int maxsize,int user_id);
extern "C" void set_comm_in(void *p);
extern "C" void set_comm_out(void *p);
extern "C" array_t *_users();

STDMETHODIMP CMudos::SetCommIn(int pFunc)
{
	// TODO: Add your implementation code here
	set_comm_in((void *)pFunc);
	return S_OK;
}

STDMETHODIMP CMudos::SetCommOut(int pFunc)
{
	// TODO: Add your implementation code here
	set_comm_out((void *)pFunc);
	return S_OK;
}


STDMETHODIMP CMudos::Shutdown()
{
	// TODO: Add your implementation code here
	shutdownMudOS(0);
	return S_OK;
}

STDMETHODIMP CMudos::get_Users(IMArray * * pVal)
{
	// TODO: Add your implementation code here
	if(!pVal)return S_FALSE;
	svalue_t st;
	st.type=T_ARRAY;
	st.u.arr=_users();
	CComObject<CMValue> *p;
	p=new CComObject<CMValue>;
	p->Create(&st);
	p->AddRef();
	*pVal=p;
	return S_OK;
}

STDMETHODIMP CMudos::GetUser(int UserId, IMObject * * pVal)
{
	// TODO: Add your implementation code here
	if(!pVal)return S_FALSE;
	svalue_t st;
	st.type=T_OBJECT;
	st.u.ob=(object_t *)UserId;
	CComObject<CMValue> *p;
	p=new CComObject<CMValue>;
	p->Create(&st);
	p->AddRef();
	*pVal=p;
	return S_OK;
}

extern "C" void WINAPI mudos_set_heartbeat(int h);
extern "C" int  WINAPI mudos_get_heartbeat();


STDMETHODIMP CMudos::get_HeartBeat(int * pVal)
{
	// TODO: Add your implementation code here
	*pVal=mudos_get_heartbeat();
	return S_OK;
}

STDMETHODIMP CMudos::put_HeartBeat(int newVal)
{
	// TODO: Add your implementation code here
	mudos_set_heartbeat(newVal);
	return S_OK;
}

STDMETHODIMP CMudos::NewValue(IMValue * * pVal)
{
	// TODO: Add your implementation code here
	if(!pVal)return S_FALSE;
	CComObject<CMValue> *p;
	p=new CComObject<CMValue>;
	p->AddRef();
	*pVal=p;
	return S_OK;
}

extern "C" object_t *i_this_player();

STDMETHODIMP CMudos::get_CurrentPlayer(IMObject * * pVal)
{
	// TODO: Add your implementation code here
    svalue_t st;
    st.type=T_OBJECT;
    st.u.ob=i_this_player();
    CComObject<CMValue> *p;
    p=new CComObject<CMValue>;
    p->Create(&st);
    p->AddRef();
    *pVal=p;

	return S_OK;
}

STDMETHODIMP CMudos::GetPlayerID(IMObject *pOb,int * pVal)
{
	// TODO: Add your implementation code here
    CComObject<CMValue> *p;
    pOb->QueryInterface(IID_IMValue,(void **)&p);
    if(!p)return S_FALSE;
    *pVal=(int)p->GetSValue()->u.ob;
    p->Release();
	return S_OK;
}
